package com.objecteffects.ilmp.web.action;

import java.io.IOException;

import com.google.inject.Inject;

import com.objecteffects.ilmp.tumblr.TumblrResponse;
import com.objecteffects.ilmp.tumblr.method.Following;
import com.objecteffects.ilmp.util.logger.InjectLogger;
import com.objecteffects.ilmp.web.utils.LoginRequired;
import com.objecteffects.ilmp.web.utils.TumblrAccessToken;

import org.scribe.model.Token;

import org.slf4j.Logger;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;

/**
 * @author Rusty Wright
 */
@LoginRequired
public class FollowingActionBean extends BaseActionBean {
    @InjectLogger
    private Logger log;

    private static final String METHOD = "user/following";

    private final TumblrAccessToken tumblrAccessToken;

    @Inject
    FollowingActionBean(final TumblrAccessToken _tumblrAccessToken) {
        this.tumblrAccessToken = _tumblrAccessToken;
    }

    @DefaultHandler
    public Resolution startHandler() throws IOException {
        final String tumblrUrl = this.getContext().getTumblrUrl();

        if (tumblrUrl == null) {
            this.log.debug("tumblrUrl is null");

            this.getContext().getMessages()
            .add(new SimpleMessage("Tumblr url is null."));

            return new RedirectResolution(TumblrStartUrlActionBean.class);
        }

        this.log.debug("tumblrUrl: {}", tumblrUrl);

        final Token accessToken =
            this.tumblrAccessToken.fetchToken(tumblrUrl, this.getContext()
                .getUserId());

        if (accessToken == null) {
            this.log.debug("accessToken: {}", accessToken);

            return new RedirectResolution(TumblrStartUrlActionBean.class);
        }

        final TumblrResponse<Following> built =
            new TumblrResponse.Builder<Following>().accessToken(accessToken)
                .blog(tumblrUrl).method(FollowingActionBean.METHOD).setUser()
                .type(Following.class).offset(Integer.valueOf(0)).build();

        final Following following = built.responseMapper();

        if (following == null) {
            this.getContext().getMessages()
            .add(new SimpleMessage("Permission denied."));

            return new ForwardResolution(IlmpView.ERROR);
        }

        this.getContext()
        .getMessages()
        .add(new SimpleMessage(String.format("following meta: %s",
                                             following.getMeta())));

        /*
         * this.getContext() .getMessages() .add(new
         * SimpleMessage(String.format("following response: %s",
         * following.getResponse())));
         */

        this.getContext()
        .getMessages()
        .add(new SimpleMessage(String.format("following response size: %d",
                                             Integer.valueOf(following
                                                             .getResponse().getBlogs()
                                                             .size()))));

        final StringBuilder sb = new StringBuilder();

        for (final Following.Blog blog : following.getResponse().getBlogs()) {
            sb.append(blog.getName());
            sb.append("<br />");
        }

        this.getContext()
        .getMessages()
        .add(new SimpleMessage(
                     String.format("following: %s", sb.toString())));

        return new ForwardResolution(IlmpView.MESSAGES);
    }
}
